package cn.enilu.flash.api.controller.order;

import cn.enilu.flash.api.controller.BaseController;
import cn.enilu.flash.bean.entity.order.PayOrder;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.service.order.PayOrderService;

import cn.enilu.flash.bean.core.BussinessLog;
import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.enumeration.BizExceptionEnum;
import cn.enilu.flash.bean.exception.ApplicationException;
import cn.enilu.flash.bean.vo.front.Ret;
import cn.enilu.flash.bean.vo.front.Rets;

import cn.enilu.flash.utils.factory.Page;


import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;


@RestController
@RequestMapping("/pay/order")
public class PayOrderController extends BaseController {
	private  Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private PayOrderService payOrderService;

	@GetMapping(value = "/list")
	@RequiresPermissions(value = "payOrder")
	public Ret list(@RequestParam(required = false) Long id) {
		Page<PayOrder> page = new PageFactory<PayOrder>().defaultPage();
		page.addFilter("id",id);
		page = payOrderService.queryPage(page);
		return Rets.success(page);
	}
	@GetMapping("getVipIncomes")
	public Ret getVipIncomes(){
		List<String> date = new LinkedList<>();
		List<String> vipIncomes = new LinkedList<>();
		List<SearchFilter> list = new ArrayList<>();
		list.add(new SearchFilter("brokerage", SearchFilter.Operator.LTE, 0));

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for(int i = 7;i>0;i--){
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE,-i);//减7天
			String format = sdf.format(cal.getTime());
			date.add(format);
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);
			Calendar tepCal = (Calendar)cal.clone();
			tepCal.add(Calendar.DATE,1);
			list.add(new SearchFilter("createTime", SearchFilter.Operator.GTE,cal.getTime()));
			list.add(new SearchFilter("createTime", SearchFilter.Operator.LTE,tepCal.getTime()));
			List<PayOrder> payOrders = payOrderService.queryAll(list);
			BigDecimal count = BigDecimal.ZERO;
			for(PayOrder payOrder:payOrders){
				count = count.add(payOrder.getTotalServiceFee());
			}
			vipIncomes.add(count.toString());
			list.remove(1);
			list.remove(1);
		}
		Map<String,Object> map = new HashMap<>();
		map.put("date",date);
		map.put("vipIncomes",vipIncomes);
		return Rets.success(map);
	}
	@PostMapping
	@BussinessLog(value = "新增支付订单类", key = "name")
	@RequiresPermissions(value = "payOrderAdd")
	public Ret add(@ModelAttribute PayOrder payOrder){
		payOrderService.insert(payOrder);
		return Rets.success();
	}
	@PutMapping
	@BussinessLog(value = "更新支付订单类", key = "name")
	@RequiresPermissions(value = "payOrderUpdate")
	public Ret update(@ModelAttribute PayOrder payOrder){
		payOrderService.update(payOrder);
		return Rets.success();
	}
	@DeleteMapping
	@BussinessLog(value = "删除支付订单类", key = "id")
	@RequiresPermissions(value = "payOrderDelete")
	public Ret remove(Long id){
		if (id == null) {
			throw new ApplicationException(BizExceptionEnum.REQUEST_NULL);
		}
		payOrderService.delete(id);
		return Rets.success();
	}
}